﻿Public Class frmTipoIdentificacionws
    Public modeForm As Integer
    Public frmParent As frmMTipoIdentificacionws


    Private Sub frmTipoIdentificacionws_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        Select Case Me.modeForm
            Case 1 'Nuevo
                frmParent.tsbNuevo.Enabled = True
            Case 2 'Consultar
                frmParent.tsbConsultar.Enabled = True
            Case 3 'Editar
                frmParent.tsbEditar.Enabled = True
        End Select
    End Sub

    Private Sub frmTipoIdentificacionws_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Me.btnCancelar.Text = "Cancelar"
        Me.btnGuardar.Visible = True

        Dim dstRecord As New DataSet
        Dim strSql As String = "SELECT tipodocumento.tipodocumento_id, tipodocumento.tipodocumento_abreviatura ||' - '|| tipodocumento.tipodocumento_nombre FROM tipodocumento ORDER BY tipodocumento.tipodocumento_abreviatura"
        dstRecord.Tables.Add("TipoIdentificacion")
        mdlCRUD.sqlRecord(dstRecord, "TipoIdentificacion", strSql)

        Me.cmbIntegracion.DataSource = dstRecord.Tables("TipoIdentificacion")
        Me.cmbIntegracion.DisplayMember = dstRecord.Tables("TipoIdentificacion").Columns(1).Caption.ToString
        Me.cmbIntegracion.ValueMember = dstRecord.Tables("TipoIdentificacion").Columns(0).Caption.ToString

        Select Case Me.modeForm
            Case 1 'Nuevo
                Me.txtCodigo.Text = ""
                Me.txtNombre.Text = ""
                Me.cmbIntegracion.SelectedValue = -1

            Case 2 'Consultar
                Me.txtCodigo.Text = Me.frmParent.arrConsulta(0)
                Me.txtNombre.Text = Me.frmParent.arrConsulta(1)

                If Me.frmParent.arrConsulta(2) <> "" Then
                    Me.cmbIntegracion.SelectedValue = Me.frmParent.arrConsulta(2)
                Else
                    Me.cmbIntegracion.SelectedIndex = -1
                End If

                Me.btnCancelar.Text = "Continuar"
                Me.btnGuardar.Visible = False

            Case 3 'Editar
                dstRecord = New DataSet
                Dim strId As String = Me.frmParent.dgvTipoIdentificacion.Rows(Me.frmParent.dgvTipoIdentificacion.CurrentRow.Index).Cells(0).Value.ToString

                dstRecord.Tables.Add("TipoIdentificacion")
                mdlConexion.blnOnDB = True
                mdlCRUD.loadRecord(dstRecord, "TipoIdentificacion", "tipoidentificacion", "tipoidentificacion_id = '" & strId & "'", "")
                mdlConexion.blnOnDB = False

                Dim arrRow As Array
                arrRow = dstRecord.Tables("TipoIdentificacion").Rows(0).ItemArray.ToArray

                Me.lblEdCodigo.Text = arrRow(0).ToString
                Me.txtCodigo.Text = arrRow(0).ToString
                Me.txtNombre.Text = arrRow(1).ToString

                If arrRow(2).ToString <> "" Then
                    Me.cmbIntegracion.SelectedValue = arrRow(2).ToString
                Else
                    Me.cmbIntegracion.SelectedValue = -1
                End If
        End Select
    End Sub

    Private Sub btnGuardar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGuardar.Click
        mdlConexion.blnOnDB = True
        Select Case Me.modeForm
            Case 1 'Nuevo
                Dim arrCampos() As String = {"tipoidentificacion_id", "tipoidentificacion_nombre", "tipoidentificacion_idcrc"}
                Dim arrValor() As String = {Trim(Me.txtCodigo.Text),
                                            Trim(Me.txtNombre.Text),
                                            IIf(Me.cmbIntegracion.SelectedIndex > -1 And Me.cmbIntegracion.SelectedValue <> Nothing, Me.cmbIntegracion.SelectedValue, "")}

                mdlCRUD.insRecord("tipoidentificacion", arrCampos, arrValor)

            Case 2 'Consultar

            Case 3 'Editar
                Dim arrParametro() As String = {"tipoidentificacion_id = " & IIf(Trim(Me.txtCodigo.Text) <> "", "'" & Me.txtCodigo.Text & "'", "null"),
                                                "tipoidentificacion_nombre = " & IIf(Trim(Me.txtNombre.Text) <> "", "'" & Me.txtNombre.Text & "'", "null"),
                                                "tipoidentificacion_idcrc = " & IIf(Me.cmbIntegracion.SelectedIndex > -1, "'" & Me.cmbIntegracion.SelectedValue & "'", "null")}
                Dim strCondicion As String = "tipoidentificacion_id = " & IIf(Me.lblEdCodigo.Text <> "", "'" & Me.lblEdCodigo.Text & "'", "null")

                mdlCRUD.updRecord("tipoidentificacion", arrParametro, strCondicion)
        End Select

        mdlConexion.blnOnDB = False

        If (mdlCRUD.blnFlag = True) Then
            Me.frmParent.refreshList()
            Me.Close()
        End If
    End Sub

    Private Sub btnCancelar_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancelar.Click
        Select Case Me.modeForm
            Case 1 'Nuevo

            Case 2 'Consultar
                Me.frmParent.arrConsulta(0) = Trim(Me.txtCodigo.Text)
                Me.frmParent.arrConsulta(1) = Trim(Me.txtNombre.Text)

                If Me.cmbIntegracion.SelectedIndex <> -1 Then
                    Me.frmParent.arrConsulta(2) = Trim(Me.cmbIntegracion.SelectedValue.ToString)
                End If

                Me.frmParent.refreshList()

            Case 3 'Editar

        End Select


        Me.Close()
    End Sub
End Class